package cn.iocoder.yudao.module.pay.bank.controller.app;

import cn.iocoder.yudao.framework.common.exception.Assertions;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.thirdparty.verify.bankcard.BankVerifyClient;
import cn.iocoder.yudao.framework.thirdparty.verify.bankcard.BankVerifyResult;
import cn.iocoder.yudao.framework.util.BankUtils;
import cn.iocoder.yudao.framework.validation.BankCard;
import cn.iocoder.yudao.module.api.pay.transfer.enums.UnifiedTransferAccountType;
import cn.iocoder.yudao.module.pay.bank.convert.BankConvert;
import cn.iocoder.yudao.module.pay.bank.dal.dataobject.PayBankDO;
import cn.iocoder.yudao.module.pay.bank.service.PayBankService;
import cn.iocoder.yudao.module.pay.bank.vo.*;
import cn.iocoder.yudao.module.pay.cashout.service.AppPayCashOutAccountService;
import cn.iocoder.yudao.module.pay.cashout.vo.AppPayCashOutAccountSaveDTO;
import cn.iocoder.yudao.module.pay.cashout.vo.AppPayCashOutAccountVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 银行卡")
@RestController
@RequestMapping("/pay/bank")
@Validated
public class AppPayBankController {

    @Resource
    private PayBankService bankService;
    @Resource
    private AppPayCashOutAccountService appPayCashOutAccountService;

    @GetMapping("/code-list")
    @Operation(summary = "获取银行代码列表")
    @PermitAll
    public CommonResult<List<PayBankCodeVO>> getBankCodeList() {
        return success(bankService.getBankCodeList());
    }

    @GetMapping("/validate")
    @Operation(summary = "校验银行卡有效性")
    @Parameter(name = "cardNo", description = "银行卡号", required = true, example = "6222807102671159575")
    @PermitAll
    public CommonResult<PayBankCardRespVO> validate(@RequestParam("cardNo") @BankCard String cardNo) {
        BankVerifyResult result = BankVerifyClient.validateCardInfo(cardNo);
        Assertions.isTrue(result.isValidated(), "银行卡号无效");
        PayBankCardRespVO respVO = BankConvert.INSTANCE.convert(result);
        List<PayBankCodeVO> bankCodes = bankService.getBankCodeList();
        for (PayBankCodeVO bankCode : bankCodes) {
            if (bankCode.getCode().equals(result.getBank())) {
                respVO.setCardName(bankCode.getName());
                break;
            }
        }
        return success(respVO);
    }

    @PostMapping("/create")
    @Operation(summary = "创建银行卡", description = "使用提现账户创建接口代替", deprecated = true)
    @Deprecated
    public CommonResult<Long> createBank(@Valid @RequestBody AppPayBankCreateReqVO createReqVO) {
        AppPayCashOutAccountSaveDTO saveVO = new AppPayCashOutAccountSaveDTO();
        BankUtils.BankBin bankBin = BankUtils.getBankBinByCardNo(createReqVO.getCardNo());
        saveVO.setBankName(bankBin == null ? createReqVO.getCardName() : bankBin.getName());
        saveVO.setAccountType(UnifiedTransferAccountType.BANK.getCode());
        saveVO.setAccountNo(createReqVO.getCardNo());
        saveVO.setBankAddress(createReqVO.getCardAddress());
        saveVO.setRealname(createReqVO.getRealName());
        saveVO.setEidNo(createReqVO.getEidNo());
        saveVO.setMobile(createReqVO.getMobile());
        Long id = appPayCashOutAccountService.create(saveVO, getLoginUserId());
        AppPayCashOutAccountVO respVO = appPayCashOutAccountService.getDetail(id);
        Long bankId = respVO.getBankId();
        return success(bankId);
    }

    @PostMapping("/update-default")
    @Operation(summary = "设置默认银行卡")
    public CommonResult<Boolean> updateDefaultBank(@Valid @RequestBody AppDefaultBankSaveDTO saveVO) {
        bankService.updateDefaultBank(getLoginUserId(), saveVO.getId());
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除银行卡", description = "使用提现账户删除接口代替", deprecated = true)
    @Parameter(name = "id", description = "编号", required = true)
    @Deprecated
    public CommonResult<Boolean> deleteBank(@RequestParam("id") Long id) {
        appPayCashOutAccountService.deleteByBankId(getLoginUserId(), id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得银行卡")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppPayBankRespVO> getBank(@RequestParam("id") Long id) {
        PayBankDO bank = bankService.getBank(id);
        return success(BankConvert.INSTANCE.convert(BankConvert.INSTANCE.convert(bank)));
    }

    @GetMapping("/get-default")
    @Operation(summary = "获得默认银行卡")
    public CommonResult<AppPayBankRespVO> getDefaultBank() {
        PayBankDO bank = bankService.getDefaultBank(getLoginUserId());
        return success(BankConvert.INSTANCE.convert(BankConvert.INSTANCE.convert(bank)));
    }

    @GetMapping("/list")
    @Operation(summary = "获得银行卡列表")
    @Parameter(name = "isDefault", description = "是否默认银行卡，不指定则返回全部", example = "false")
    public CommonResult<List<AppPayBankRespVO>> getBankList(@RequestParam(required = false) Boolean isDefault) {
        List<PayBankDO> list = bankService.getBankList(getLoginUserId(), isDefault);
        return success(BankConvert.INSTANCE.convertList(list));
    }

}
